package com.ruoyi.business.service.impl;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.Objects;
import java.util.Set;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.business.domain.TrainStation;
import com.ruoyi.business.domain.dto.TrainStationGroupDTO;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.TrainMapper;
import com.ruoyi.business.domain.Train;
import com.ruoyi.business.service.ITrainService;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

/**
 * 火车管理Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class TrainServiceImpl implements ITrainService 
{
    @Autowired
    private TrainMapper trainMapper;

    /**
     * 查询火车管理
     * 
     * @param id 火车管理主键
     * @return 火车管理
     */
    @Override
    public Train selectTrainById(Long id)
    {
        return trainMapper.selectTrainById(id);
    }

    /**
     * 查询火车管理列表
     * 
     * @param train 火车管理
     * @return 火车管理
     */
    @Override
    public List<Train> selectTrainList(Train train)
    {
        return trainMapper.selectTrainList(train);
    }

    /**
     * 新增火车管理
     * 
     * @param train 火车管理
     * @return 结果
     */
    @Override
    public int insertTrain(Train train)
    {
        train.setCreateTime(DateUtils.getNowDate());
        return trainMapper.insertTrain(train);
    }

    /**
     * 修改火车管理
     * 
     * @param train 火车管理
     * @return 结果
     */
    @Override
    public int updateTrain(Train train)
    {
        train.setUpdateTime(DateUtils.getNowDate());
        return trainMapper.updateTrain(train);
    }

    /**
     * 批量删除火车管理
     * 
     * @param ids 需要删除的火车管理主键
     * @return 结果
     */
    @Override
    public int deleteTrainByIds(Long[] ids)
    {
        return trainMapper.deleteTrainByIds(ids);
    }

    /**
     * 删除火车管理信息
     * 
     * @param id 火车管理主键
     * @return 结果
     */
    @Override
    public int deleteTrainById(Long id)
    {
        return trainMapper.deleteTrainById(id);
    }

    /**
     * 批量新增火车管理
     * @param TrainList
     * @return 结果
     */

    @Override
    public int insertTrains(List<Train> TrainList) {
        return trainMapper.insertTrains(TrainList);
    }


    /**
     * 根据车次编号查询火车信息
     *
     * @param code 车次编号
     * @return 火车信息
     */
    @Override
    public Train selectTrainByCode(String code) {
        return trainMapper.selectTrainByCode(code);
    }

    /**
     * 下载火车管理导入模板 - 不包含错误字段
     */
    @Override
    public void downloadCustomTemplate(HttpServletResponse response) throws IOException {
        // 创建自定义的工作簿和工作表
        SXSSFWorkbook wb = new SXSSFWorkbook(500);
        try {
            Sheet sheet = wb.createSheet("火车管理数据");
            
            // 创建表头行并设置样式
            Row headerRow = sheet.createRow(0);
            CellStyle headerStyle = wb.createCellStyle();
            headerStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
            headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            headerStyle.setBorderBottom(BorderStyle.THIN);
            headerStyle.setBorderLeft(BorderStyle.THIN);
            headerStyle.setBorderRight(BorderStyle.THIN);
            headerStyle.setBorderTop(BorderStyle.THIN);
            headerStyle.setAlignment(HorizontalAlignment.CENTER);
            
            Font headerFont = wb.createFont();
            headerFont.setBold(true);
            headerStyle.setFont(headerFont);
            
            // 设置含ID的标题行
            String[] headers = {"ID", "车次编号", "车次类型", "始发站", "始发站拼音", "出发时间", "终点站", "终点站拼音", "到站时间"};
            for (int i = 0; i < headers.length; i++) {
                Cell headerCell = headerRow.createCell(i);
                headerCell.setCellValue(headers[i]);
                headerCell.setCellStyle(headerStyle);
                sheet.setColumnWidth(i, 5000); // 设置合适的列宽
            }
            
            // 创建数据行样式
            CellStyle dataStyle = wb.createCellStyle();
            dataStyle.setBorderBottom(BorderStyle.THIN);
            dataStyle.setBorderLeft(BorderStyle.THIN);
            dataStyle.setBorderRight(BorderStyle.THIN);
            dataStyle.setBorderTop(BorderStyle.THIN);
            dataStyle.setAlignment(HorizontalAlignment.CENTER); // 居中对齐
            
            // 添加示例数据
            Row exampleRow = sheet.createRow(1);
            
            // ID示例
            Cell cell0 = exampleRow.createCell(0);
            cell0.setCellValue("1890636568357368001");
            cell0.setCellStyle(dataStyle);
            
            // 车次编号示例
            Cell cell1 = exampleRow.createCell(1);
            cell1.setCellValue("G1234");
            cell1.setCellStyle(dataStyle);
            
            // 车次类型示例
            Cell cell2 = exampleRow.createCell(2);
            cell2.setCellValue("1");
            cell2.setCellStyle(dataStyle);
            
            // 始发站示例
            Cell cell3 = exampleRow.createCell(3);
            cell3.setCellValue("北京西站");
            cell3.setCellStyle(dataStyle);
            
            // 始发站拼音示例
            Cell cell4 = exampleRow.createCell(4);
            cell4.setCellValue("beijingxizhan");
            cell4.setCellStyle(dataStyle);
            
            // 出发时间示例
            Cell cell5 = exampleRow.createCell(5);
            cell5.setCellValue("08:30:00");
            cell5.setCellStyle(dataStyle);
            
            // 终点站示例
            Cell cell6 = exampleRow.createCell(6);
            cell6.setCellValue("上海虹桥站");
            cell6.setCellStyle(dataStyle);
            
            // 终点站拼音示例
            Cell cell7 = exampleRow.createCell(7);
            cell7.setCellValue("shanghaihongqiaozhan");
            cell7.setCellStyle(dataStyle);
            
            // 到站时间示例
            Cell cell8 = exampleRow.createCell(8);
            cell8.setCellValue("14:30:00");
            cell8.setCellStyle(dataStyle);
            
            // 输出到响应
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            String fileName = "train_template.xlsx";
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            
            wb.write(response.getOutputStream());
        } finally {
            wb.dispose();
        }
    }

    /**
     * 清空火车数据
     * 
     * @return 结果
     */
    @Override
    public int clearTrain() {
        return trainMapper.clearTrain();
    }
}
